/**
 * 仪表盘相关 API
 */
import http from '../utils/request'

export interface DashboardOverview {
  devices: {
    total: number
    online: number
    offline: number
    fault: number
    by_status: Record<string, number>
  }
  work_orders: {
    total_count: number
    pending_count: number
    in_progress_count: number
    completed_count: number
    overdue_count: number
    avg_resolution_time: number
    completion_rate: number
  }
  alerts: {
    total_count: number
    unresolved_count: number
    resolved_count: number
    critical_count: number
    high_count: number
    avg_resolution_time: number
    resolution_rate: number
    by_type: Record<string, number>
    by_level: Record<string, number>
  }
  materials: {
    low_level_devices: number
    avg_level: number
  }
  summary: {
    total_devices: number
    online_devices: number
    pending_work_orders: number
    unresolved_alerts: number
    critical_alerts: number
  }
}

export interface RealTimeData {
  device_status: {
    online_count: number
    total_count: number
    online_rate: number
  }
  recent_alerts: Array<{
    id: number
    alert_number: string
    title: string
    level: string
    alert_type: string
    device_name?: string
    created_at: string
  }>
  recent_work_orders: Array<{
    id: number
    work_order_number: string
    title: string
    status: string
    priority: string
    device_name?: string
    created_at: string
  }>
}

export interface TrendData {
  work_order_trend: Array<{
    date: string
    count: number
  }>
  alert_trend: Array<{
    date: string
    count: number
  }>
  date_range: {
    start_date: string
    end_date: string
  }
}

// 获取系统概览数据
export const getDashboardOverview = () => {
  return http.get<DashboardOverview>('/dashboard/overview')
}

// 获取实时数据
export const getRealTimeData = () => {
  return http.get<RealTimeData>('/dashboard/real-time')
}

// 获取趋势数据
export const getTrendData = () => {
  return http.get<TrendData>('/dashboard/trends')
}